<?php
declare(strict_types=1);

namespace App\Controller\Api\V1\Group;

use App\Controller\Api\V1\CController;
use App\Model\LiveGift;
use App\Model\PlayersGift;
use App\Model\UsersGift;
use App\Service\Group\GroupMemberService;
use App\Service\Group\GroupNoticeService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware;
use Psr\Http\Message\ResponseInterface;

/**
 * Class NoticeController
 *
 * @Controller(prefix="/api/v1/group/gift")
 * @Middleware(JWTAuthMiddleware::class)
 *
 * @package App\Controller\Api\V1\Group
 */
class GiftController extends CController
{
    /**
     * @var GroupMemberService
     */
    private $groupMemberService;

//    public function __construct(GroupMemberService $groupMemberService)
//    {
//        parent::__construct();
//
//        $this->groupMemberService = $groupMemberService;
//    }

    /**
     * 获取所有礼物
     *
     * @RequestMapping(path="list", methods="get")
     */
    public function list(): ResponseInterface
    {
//        $group_id = (int)$this->request->input('group_id', 0);

        $r = LiveGift::where('status', '=', 100)->orderBy('points', 'asc')->get()->toArray();

        return $this->response->success([
            "rows" => $r,
        ]);
    }

    /**
     * 选手礼物榜单
     *
     * @RequestMapping(path="player", methods="get")
     */
    public function players() : ResponseInterface
    {
        $params = $this->request->inputs(['page_size', 'page', 'activity_id', 'package_id']);

        $page = data_get($params, 'page', 20);
        $pageSize = data_get($params, 'page_size', 1);
        $activityId = (int)$this->request->input('activity_id', 0);
        $model = PlayersGift::where('players_gift.activity_id', '=', $activityId)
            ->where('players_gift.package_id', '=', $params['package_id'])
            ->join('players as p', 'p.sign_id', 'players_gift.player_id');
//            ->leftjoin('user_detail as u', 'u.user_id', '=', 'p.user_id');

        $count = $model->count();

        $rows  = [];
        if ($count > 0) {
            $model->orderBy('players_gift.points', 'desc');

            $rows = $model->forPage($page, $pageSize)->get(['players_gift.player_id', 'p.head_img', 'p.name', 'players_gift.points'])->toArray();
        }

        return $this->response->success($rows);
    }

    /**
     * 用户发礼物榜单
     *
     * @RequestMapping(path="user", methods="get")
     */
    public function users() : ResponseInterface
    {
        $params = $this->request->inputs(['page_size', 'page', 'package_id']);

        $page = data_get($params, 'page', 1);
        $pageSize = data_get($params, 'page_size', 20);
        $activityId = (int)$this->request->input('activity_id', 0);
        $model = UsersGift::where('users_gift.activity_id', '=', $activityId)
            ->where('users_gift.package_id', '=', $params['package_id'])
            ->join('users_live as u', 'u.user_id', '=', 'users_gift.user_id');

        $count = $model->count();

        $rows  = [];
        if ($count > 0) {
            $model->orderBy('users_gift.points', 'desc');

            $rows = $model->forPage($page, $pageSize)->get(['u.user_id', 'u.nickname', 'u.head_img', 'users_gift.points'])->toArray();
        }

        return $this->response->success($rows);
    }





}
